<?php
namespace Admin\Controller;

class GoodsController extends CommonController {
    public function index(){
        $this->assign('title','商品管理');
        $model = M('goods');
        $data = $model->select();
        $this->assign('goodslist',$data);
        $this->show();
    }

    public function add(){
        //POST数据是否为空
        if (empty($_POST)) {
            $this->display('Goods/add');
            exit; 
        }
        //生成SQL数据
        M('goods')->create();
        //返回自增ID
        if (M('goods')->add() > 0) {
            $this->success('添加成功!', U('index'));
        } else {
            $this->error('添加失败鸟~~');
        }
    }

    public function insert(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/Store/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        $upload->subName = '';
        // 上传文件 
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{
            foreach($info as $file){
                $g_icon =  $file['savename'];
            }
        }

        $goods = M('goods');
        // $goods->g_icon = date('Ymd',time()).'_'.mt_rand().'.'.$info[0]['extension'];
        $goods->g_icon = $g_icon;
        $goods->g_name = I('g_name');
        $goods->price =I('price');
        $goods->g_rank = I('g_rank');
        $goods->up = I('up');
        $goods->g_detail = I('g_detail');
                
        if ($goods->add() > 0) {
                $this->success('添加成功!', U('Admin/Goods/index'));
            } else {
                $this->error('添加失败~~');
            }
    }

    public function delete(){
        $model = M('goods');
        $model->delete($_POST['gid']);
    }

    public function editUp(){
        $model = M('goods');
        $data['up'] = $_POST['up'];
        $a = $model->where('g_id = "'.$_POST['gid'].'"')->save($data);
        echo json_encode($a);
    }

    public function edit(){
        $gid = I('get.gid');
        $model = M('goods');
        $data = $model->where('g_id="'.$gid.'"')->select();
        $this->assign('goods',$data[0]);
        $this->display();
    }

    public function updata(){
        //生成数据
        M('goods')->create();
        if (M('goods')->save()) {
            $this->success('编辑成功!', U('Admin/Goods/index'));
        } else {
            $this->error('编辑失败鸟~~');
        }
    }
}